package xhwl.insurance.ui;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import xhwl.android.common.util.SPUtil;
import xhwl.insurance.R;
import xhwl.insurance.BaseActivity;
import xhwl.insurance.adapter.My_SxjgAdapter;
import xhwl.insurance.http.api.MarketerApi;
import xhwl.insurance.http.bean.resp.TwoSaleResp;
import xhwl.insurance.util.XHLog;
import xhwl.retrofitrx.HttpException;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

/**
 * Created by chengang on 2017/7/10.
 *  我的销售架构
 */

public class My_SxjgActivity extends BaseActivity {

    private TextView marketer;
    private ListView marketerList;
    private ArrayList<TwoSaleResp> twoSaleResps = new ArrayList<>();
    private My_SxjgAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_xsjg);
    }

    @Override
    public void initViews() {
        initTitleViews();
        marketer = (TextView) findViewById(R.id.marketer);
        marketerList = (ListView) findViewById(R.id.marketer_list);
        marketer.setText("我是哪些人的间接销售者(0人)");
        adapter = new My_SxjgAdapter(this,twoSaleResps);
        marketerList.setAdapter(adapter);

    }

    @Override
    protected void setUpTitleView() {
        titleTextView.setText("我的销售架构");

    }

    @Override
    public void initListener() {

    }

    @Override
    public void loadData() {
        requestList();
    }



    private void requestList() {
        String member_id = SPUtil.getPersonInfoSp().getString(SPUtil.SPPersonInfo.MEMBER_ID,"");
        MarketerApi api = new MarketerApi(member_id);

        HttpManager.getInstance().dealHttp(this, api, new OnNextListener<ArrayList<TwoSaleResp>>() {

            @Override
            public void onNext(ArrayList<TwoSaleResp> resp) {
                try {
                    marketer.setText("我是哪些人的间接销售者(" + resp.size() + "人)" );
                    twoSaleResps.addAll(resp);
                    adapter.notifyDataSetChanged();
                } catch (Exception e) {
                    XHLog.e(e.getMessage());
                }
            }

            @Override
            public void onError(HttpException ex) {
                super.onError(ex);
            }
        });
    }

}
